#include "NatCalculatorParam.h"
#include <list>
#include <core/time/system_time.h>
#include <bondlib/SSVContainer.h>

typedef qb::base::CTime CTime;

CNatCalculatorParam::CNatCalculatorParam()
{
}

CNatCalculatorParam::~CNatCalculatorParam()
{
}

bool CNatCalculatorParam::InsertConvertFactor(const xConvertFactorsAck_c* pAck)
{
	SCOPED_MUTEX(m_mutex);
	if (!pAck) return false;
	std::string stTemp;
	for (std::list<xConvertFactorsUnit_c>::const_iterator itr = pAck->m_List.begin();
		itr != pAck->m_List.end(); itr++){
		getCombineKey(stTemp, itr->m_deliverDate, itr->m_bondKey);
		if (m_mapFactor.find(stTemp) == m_mapFactor.end()){
			m_mapFactor.insert(make_pair(stTemp, itr->m_convertFactors));
		}
	}
	return true;
}

bool CNatCalculatorParam::InsertConvertFactor(const char* m_deliverDate, const char* cBondKey, double dbFac)
{
	SCOPED_MUTEX(m_mutex);
	if (strlen(cBondKey) > 0 && strlen(m_deliverDate) > 0){
		std::string stTemp;
		getCombineKey(stTemp, m_deliverDate, cBondKey);
		if (m_mapFactor.find(stTemp) == m_mapFactor.end()){
			m_mapFactor.insert(make_pair(stTemp, dbFac));
			return true;
		}
	}
	return false;
}

bool CNatCalculatorParam::GetBondConvertFactor(const char* m_deliverDate, const char* cBondKey, double& dbFac)
{
	SCOPED_MUTEX(m_mutex);
	std::string stTemp;
	getCombineKey(stTemp, m_deliverDate, cBondKey);
	if (m_mapFactor.find(stTemp) != m_mapFactor.end()){
		dbFac = m_mapFactor[stTemp];
		return true;
	}
	return false;
}

void CNatCalculatorParam::getCombineKey(std::string& stKey, const char* m_deliverDate, const char* cBondKey)
{
	CTime ctTime(CSSVContainer::GetServerTime());
	int nDate = ctTime.GetYear() * 10000 + ctTime.GetMonth() * 100 + ctTime.GetDay();
	char cDate[10] = { 0 };
	sprintf(cDate, "%d", nDate);
	stKey = std::string(cDate) + std::string(m_deliverDate) + std::string(cBondKey);
}
